/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.cachemanager.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.buttonmanager.service.PlatButtonService;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

@Component("system.modules.system.cache.ButtonCacheService")
public class ButtonCacheService {
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	Cache buttonCache = null;
	
	public Cache getCache(){
		if(buttonCache == null){
			buttonCache = cacheManager.getCache("ButtonCache");
		}
		return buttonCache;
	}
	
	@Autowired
	PlatButtonService buttonService;
	
	public void init(){
		List<PlatButton> buttons = buttonService.getAllButtons();
		for(PlatButton button : buttons){
			Element em = new Element(button.getBtnId(), button);
			getCache().put(em);
		}
		Log.info("初始化按钮缓存数量:"+buttons.size());
	}
}
